//
//  YZCommunityWebViewModel.m
//  VET
//
//  Created by YZ on 2018/7/18.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZCommunityWebViewModel.h"
#import "YZCommunityAddFriendVC.h"
#import "YZCommunityMineVC.h"
#import "YZCommunityDetailVC.h"
#import "YZChatVC.h"
#import "YZChatPeer.h"
#import "YZActionSheet.h"
#import "YZApplyFriendVC.h"
#import "YZSearchFriendVC.h"
#import "YYPhotoGroupView.h"
#import "YZShowNetImagesVC.h"
#import "TZVideoPlayerController.h"
#import "YZNetworkConfig.h"

@interface YZCommunityWebViewModel()<YZActionSheetDelegate>

@property (nonatomic, strong) NSArray *currentTitles;

@property (nonatomic, copy) NSString *itemId;

@property (nonatomic, strong) YZBaseWebViewController *currentVC;

@end

@implementation YZCommunityWebViewModel

- (void)doActionWithMessage:(NSString *)message currentController:(YZBaseWebViewController *)currentVC
{
    NSString *domain = [YZNetworkConfig getUrlCurrentDomain];
    self.currentVC = currentVC;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.currentVC.navigationItem.backBarButtonItem = backItem;
    NSDictionary *functionObj = [NSDictionary dictionaryWithJsonString:message];
    NSString *functionStr = functionObj[kActionKey];
    NSDictionary *data = functionObj[kDataKey];
    if ([functionStr isEqualToString:@"getToken"]) {
        NSString *token = [YZUser sharedInstance].token;
        NSString *jsonStr = [@{@"token":kText(token)} TransToJSONString];
        [currentVC.webView evaluateJavaScript:[NSString stringWithFormat:@"javascript:yz.native2js({callback:\"onGetToken\",data:%@})",jsonStr] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"调用成功");
            }
        }];
    }else if ([functionStr isEqualToString:@"operation"]){
        self.currentTitles= data[@"actionArray"];
        self.itemId = data[@"itemId"];
        YZActionSheet *sheet = [[YZActionSheet alloc] initWithTitle:nil sheetTitles:self.currentTitles cancleBtnTitle:@"取消" sheetStyle:YZActionSheetDefault delegate:self];
        [sheet show];
    }else if ([functionStr isEqualToString:@"chat"]){
        
        long long uid = [data[@"uid"] longLongValue];
        NSString *receiver = [NSString stringWithFormat:@"%lld",uid] ;
        NSString *avatar = data[@"avatar"];
        YZChatVC *chatVC = [YZChatVC new];
        
        // 聊天
        [YZChatPeer shared].sender = [YZUser sharedInstance].uid;
        [YZChatPeer shared].receiver = receiver;
        [YZChatPeer shared].receiverAvatar = avatar;
        [currentVC.navigationController pushViewController:chatVC animated:YES];
    }else if ([functionStr isEqualToString:@"addFriend"]){
        // 添加好友
        NSString *uid = data[@"uid"];
        YZApplyFriendVC *applyFriendVC = [[YZApplyFriendVC alloc] init];
        applyFriendVC.targetId = uid;
        
        [currentVC.navigationController pushViewController:applyFriendVC animated:YES];
        
    } else if ([functionStr isEqualToString:@"searchFriend"]) {
        // 搜索好友
        YZSearchFriendVC *serachFriend = [[YZSearchFriendVC alloc] init];
        
        [currentVC.navigationController pushViewController:serachFriend animated:YES];
        
    } else if ([functionStr isEqualToString:@"goback"]) {
        // 回退
        if (currentVC.webView.canGoBack == YES) {
            
            [currentVC.webView goBack];
            
        } else {
            
            [currentVC.navigationController popViewControllerAnimated:YES];
            
        }
    } else if ([functionStr isEqualToString:@"circleDetail"]) {
        // 圈子详情
        NSString *item = data[@"itemId"];
        NSString *communityDetailUrl = [NSString stringWithFormat:@"%@/#/circle/%@",domain,item];
        
        YZCommunityDetailVC *detailVC = [[YZCommunityDetailVC alloc] initWithUrl:communityDetailUrl registerFunctions:nil];
        [currentVC.navigationController pushViewController:detailVC animated:YES];
        
    }  else if ([functionStr isEqualToString:@"profile"]) {
        /* 个人主页 */
        if ([data[@"tab"] intValue] == 0) {
            // 我的圈子
            NSString *communityMineUrl = [NSString stringWithFormat:@"%@/#/profile?uid=%@&tab=0",domain,[YZUser sharedInstance].uid];
            YZCommunityMineVC *mineVC = [[YZCommunityMineVC alloc] initWithUrl:communityMineUrl registerFunctions:nil];
            [currentVC.navigationController pushViewController:mineVC animated:YES];
            
        }
        else if ([data[@"tab"] intValue] == 1) {
            // 圈子消息
            NSString *communityMessageUrl = [NSString stringWithFormat:@"%@/#/profile?uid=%@&tab=1",domain,[YZUser sharedInstance].uid];
            
            YZCommunityMineVC *detailVC = [[YZCommunityMineVC alloc] initWithUrl:communityMessageUrl registerFunctions:nil];
            [currentVC.navigationController pushViewController:detailVC animated:YES];
        } 
    } else if ([functionStr isEqualToString:@"showImg"]) {
        NSArray *imagsArr = data[@"imgs"];
        NSInteger imgIndex = [data[@"imgIndex"] integerValue];
        YZShowNetImagesVC *showImageVC = [[YZShowNetImagesVC alloc] initWithImageUrls:imagsArr andSelectIndex:imgIndex];
        [currentVC.navigationController pushViewController:showImageVC animated:YES];
    }else if ([functionStr isEqualToString:@"playVideo"]){
        NSString *url = data[@"src"];
        TZVideoPlayerController *videoPlayVC = [[TZVideoPlayerController alloc] init];
        videoPlayVC.videoUrl = url;
        [currentVC presentViewController:videoPlayVC animated:YES completion:nil];
    }
}

#pragma mark -- YZActionSheetDelegate

- (void)actionSheet:(YZActionSheet *)actionSheet clickButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *actionStr = self.currentTitles[buttonIndex];
    NSString *jsonStr = [@{@"action":actionStr,@"itemId":self.itemId} TransToJSONString];
    [self.currentVC.webView evaluateJavaScript:[NSString stringWithFormat:@"javascript:yz.native2js({callback:\"onOperation\",data:%@})",jsonStr] completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"调用成功");
        }
    }];
}
@end
